﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Net_SCADA.Forms;
using Net_SCADA.Siemens;

namespace Net_SCADA
{

    public partial class Form_LimitSet : fdBaseForm
    {
        List<setButton> selectButton = new List<setButton>()
        {
            new setButton(){
                btnName=new string[]{ "公差设置", "Tolerance setting" },
                btnLocation=new Point(2,2),
                setDescription=new string[2][] { new string[2] {"上公差", "Upper tolerance" }, new string[2] {"下公差", "Lower tolerance" }, }
            },
             new setButton(){
                btnName=new string[]{ "过热设置", "Overheat setting" },
                btnLocation=new Point(154,2),
                setDescription=new string[2][] { new string[2] {"温度上限", "Upper Temperature" }, new string[2] { "", "" }, }
            },
              new setButton(){
                btnName=new string[]{ "误差设置", "Deviation setting" },
                btnLocation=new Point(306,2),
                setDescription=new string[2][] { new string[2] {"温度误差", "Temperature Deviation" },new string[2] { "", "" }, }
            },
               new setButton(){
                btnName=new string[]{ "其他参数", "Other Parameters" },
                btnLocation=new Point(458,2),
                setDescription=new string[2][] { new string[2] {"参数值", "Parameters" }, new string[2] { "", "" }, }
            },

        };

        
        private Label lblSelected=null;
        private Label LastSelected=null;
        private int GroupInterval=100;
        private int SelectIndex=0;
        private int LastSelectIndex=0;
        int designCols=8;
        int w;
        private List<Label> setBox_U=new List<Label>();//默认使用
        private List<Label> setBox_L=new List<Label>();//公差时使用
        public Form_LimitSet()
        {
            InitializeComponent();
        }

        private void Form_LimitSet_Load(object sender , EventArgs e)
        {
            netForms.SetPostionAndSize(this);
            w = (this.Width - designCols * 5 - 5) / designCols;
            LoadSelectButton();
            LoadInterface();
            LoadBaisInterface(selectButton[0]);
            txt_SetValue.LostFocus += txt_SetValue_LostFocus;
        }


        #region 初始设置
        private void LoadInterface()
        {
            txt_SetValue.Font = new Font("Arial", 15f , FontStyle.Bold);
            txt_SetValue.BackColor = Color.PaleTurquoise;
            txt_SetValue.ForeColor = Color.FromArgb(64 , 64 , 64);
        }
        private void LoadSelectButton()
        {
            for (int i = 0 ; i < selectButton.Count ; i++)
            {
                selectButton[i].getSetButton(i);
                pnl_Select.Controls.Add(selectButton[i].button);
                selectButton[i].button.Click += selectButton_Click;
            }
            selectButton[0].button.BackColor = Color.Aqua;
        }

        #endregion

        #region Bais 设置

        //每行加载designCols-1个参数
        private void LoadBaisInterface(setButton setbutton)
        {
            GroupInterval = 100;
            NetLine.FillZoneBais();
            int rows=-1;
            int total=-1;
            setBox_U.Clear();
            setBox_L.Clear();
            for (int i = 0 ; i < NetLine.zone.Count ; i++)
            {
                if (NetLine.zone[i].ubais == null) continue;
                total++;
                if (total % (designCols - 1) == 0)
                {
                    rows++;
                    pnl_Set.Controls.Add(GetItemLabel(setbutton.setTitle[formLanguageIndex], new Size(w , 20) , new Point(5 , 2 + GroupInterval * rows)));
                    pnl_Set.Controls.Add(GetItemLabel(setbutton.setDescription[0][formLanguageIndex], new Size(w , 35) , new Point(5 , 21 + GroupInterval * rows)));
                    pnl_Set.Controls.Add(GetItemLabel(setbutton.setDescription[1][formLanguageIndex], new Size(w , 35) , new Point(5 , 55 + GroupInterval * rows)));
                }
                pnl_Set.Controls.Add(GetItemLabel(NetLine.zone[i].ZoneName[formLanguageIndex] , new Size(w , 20) , new Point(10 + w + (w + 5) * (total % (designCols - 1)) , 2 + GroupInterval * rows)));
                setBox_U.Add(GetItemBox(NetLine.zone[i].ubais.ItemValue.ToString() , new Size(w , 35) , new Point(10 + w + (w + 5) * (total % (designCols - 1)) , 21 + GroupInterval * rows) , NetLine.zone[i].ubais));
                setBox_L.Add(GetItemBox(NetLine.zone[i].lbais.ItemValue.ToString() , new Size(w , 35) , new Point(10 + w + (w + 5) * (total % (designCols - 1)) , 55 + GroupInterval * rows) , NetLine.zone[i].lbais));
                setBox_U.Last().Click += setBox_Click;
                setBox_L.Last().Click += setBox_Click;
            }
            pnl_Set.Controls.AddRange(setBox_U.ToArray());
            pnl_Set.Controls.AddRange(setBox_L.ToArray());
        }
        private void SendBaisValues()
        {
            List<object>v=new List<object>();
            Item itm=new Item();
            for (int i = 0 ; i < setBox_U.Count ; i++)
            {
                itm = setBox_U[i].Tag as Item;
                v.AddRange(itm.GetPLCSetValue(setBox_U[i].Text));
                itm = setBox_L[i].Tag as Item;
                v.AddRange(itm.GetPLCSetValue(setBox_L[i].Text));
            }
            NetLine.hsl_Bais.SetValues = v;
            NetLine.hsl_Bais.IsNeedSetValue = true;
        }

        #endregion

        #region Diff 设置
        private void LoadDiffInterface(setButton setbutton)
        {
            NetLine.FillZoneDiff();
            GroupInterval = 65;
            int rows=-1;
            int total=-1;
            setBox_U.Clear();
            for (int i = 0 ; i < NetLine.zone.Count ; i++)
            {
                if (NetLine.zone[i].diff == null) continue;
                total++;
                if (total % (designCols - 1) == 0)
                {
                    rows++;
                    pnl_Set.Controls.Add(GetItemLabel(setbutton.setTitle[formLanguageIndex], new Size(w , 20) , new Point(5 , 2 + GroupInterval * rows)));
                    pnl_Set.Controls.Add(GetItemLabel(setbutton.setDescription[0][formLanguageIndex], new Size(w , 35) , new Point(5 , 21 + GroupInterval * rows)));
                }
                pnl_Set.Controls.Add(GetItemLabel(NetLine.zone[i].ZoneName[formLanguageIndex] , new Size(w , 20) , new Point(10 + w + (w + 5) * (total % (designCols - 1)) , 2 + GroupInterval * rows)));
                setBox_U.Add(GetItemBox(NetLine.zone[i].diff.ItemValue.ToString() , new Size(w , 35) , new Point(10 + w + (w + 5) * (total % (designCols - 1)) , 21 + GroupInterval * rows) , NetLine.zone[i].diff));
                setBox_U.Last().Click += setBox_Click;
            }
            pnl_Set.Controls.AddRange(setBox_U.ToArray());
        }
        private void SendDiffValues()
        {
            List<object>v=new List<object>();
            Item itm=new Item();
            for (int i = 0 ; i < setBox_U.Count ; i++)
            {
                itm = setBox_U[i].Tag as Item;
                v.AddRange(itm.GetPLCSetValue(setBox_U[i].Text));
            }
            NetLine.hsl_Diff.SetValues = v;
            NetLine.hsl_Diff.IsNeedSetValue = true;
        }

        #endregion

        #region othersetpoint 设置
        private void LoadOtherSetpointInterface(setButton setbutton)
        {
            NetLine.FillOtherSetPoint();
            GroupInterval = 65;
            int rows=-1;
            int total=-1;
            setBox_U.Clear();
            for (int i = 0 ; i < NetLine.othersetpoint.Count ; i++)
            {
                if (NetLine.othersetpoint[i].sv == null) continue;
                total++;
                if (total % (designCols - 1) == 0)
                {
                    rows++;
                    pnl_Set.Controls.Add(GetItemLabel(setbutton.setTitle[formLanguageIndex], new Size(w , 20) , new Point(5 , 2 + GroupInterval * rows)));
                    pnl_Set.Controls.Add(GetItemLabel(setbutton.setDescription[0][formLanguageIndex], new Size(w , 35) , new Point(5 , 21 + GroupInterval * rows)));
                }
                pnl_Set.Controls.Add(GetItemLabel(NetLine.othersetpoint[i].ZoneName[formLanguageIndex] , new Size(w , 20) , new Point(10 + w + (w + 5) * (total % (designCols - 1)) , 2 + GroupInterval * rows)));
                setBox_U.Add(GetItemBox(NetLine.othersetpoint[i].sv.ItemValue.ToString() , new Size(w , 35) , new Point(10 + w + (w + 5) * (total % (designCols - 1)) , 21 + GroupInterval * rows) , NetLine.othersetpoint[i].sv));
                setBox_U.Last().Click += setBox_Click;
            }
            pnl_Set.Controls.AddRange(setBox_U.ToArray());
        }
        private void SendOtherSetpointValues()
        {
            List<object>v=new List<object>();
            Item itm=new Item();
            for (int i = 0 ; i < setBox_U.Count ; i++)
            {
                itm = setBox_U[i].Tag as Item;
                v.AddRange(itm.GetPLCSetValue(setBox_U[i].Text));
            }
            NetLine.hsl_OtherSetpoint.SetValues = v;
        }

        #endregion


        #region OVSV 设置
        private void LoadOVSVInterface(setButton setbutton)
        {
            NetLine.FillZoneOVSV();
            GroupInterval = 65;
            int rows=-1;
            int total=-1;
            setBox_U.Clear();
            for (int i = 0 ; i < NetLine.zone.Count ; i++)
            {
                if (NetLine.zone[i].ovsv == null) continue;
                total++;
                if (total % (designCols - 1) == 0)
                {
                    rows++;
                    pnl_Set.Controls.Add(GetItemLabel(setbutton.setTitle[formLanguageIndex], new Size(w , 20) , new Point(5 , 2 + GroupInterval * rows)));
                    pnl_Set.Controls.Add(GetItemLabel(setbutton.setDescription[0][formLanguageIndex], new Size(w , 35) , new Point(5 , 21 + GroupInterval * rows)));
                }
                pnl_Set.Controls.Add(GetItemLabel(NetLine.zone[i].ZoneName[formLanguageIndex] , new Size(w , 20) , new Point(10 + w + (w + 5) * (total % (designCols - 1)) , 2 + GroupInterval * rows)));
                setBox_U.Add(GetItemBox(NetLine.zone[i].ovsv.ItemValue.ToString() , new Size(w , 35) , new Point(10 + w + (w + 5) * (total % (designCols - 1)) , 21 + GroupInterval * rows) , NetLine.zone[i].ovsv));
                setBox_U.Last().Click += setBox_Click;
            }
            pnl_Set.Controls.AddRange(setBox_U.ToArray());
        }
        private void SendOVSVValues()
        {
            List<object>v=new List<object>();
            Item itm=new Item();
            for (int i = 0 ; i < setBox_U.Count ; i++)
            {
                itm = setBox_U[i].Tag as Item;
                v.AddRange(itm.GetPLCSetValue(setBox_U[i].Text));
            }
            NetLine.hsl_OVSV.SetValues = v;
            NetLine.hsl_OVSV.IsNeedSetValue = true;
        }

        #endregion

        #region 基本控件控制
        private void setBox_Click(object sender , EventArgs e)
        {
            if (LastSelected != null && LastSelected.Tag!=null)
                CheckValue(LastSelected);
            lblSelected = sender as Label;
            txt_SetValue.Visible = true;
            txt_SetValue.Text = lblSelected.Text;
            txt_SetValue.BringToFront();
            txt_SetValue.SelectAll();
            txt_SetValue.Tag = lblSelected.Tag;
            txt_SetValue.Width = lblSelected.Width - 4;
            txt_SetValue.Location = new Point(lblSelected.Left + 2 , lblSelected.Top + (lblSelected.Height - txt_SetValue.Height) / 2);
            LastSelected = lblSelected;
        }


        private void CheckValue(Label lbl)
        {
            Item itm=lbl.Tag as Item;
            lbl.Text = lbl.Text.Trim();
            if (lbl.Text == "")
            {
                lbl.Text = "0";
                return;
            }
            if (Convert.ToSingle(lbl.Text) > itm.Max)
            {
                lbl.Text = itm.Max.ToString();
                return;
            }
            if (Convert.ToSingle(lbl.Text) < itm.Min)
            {
                lbl.Text = itm.Min.ToString();
                return;
            }
            if (itm.DataType.ToUpper() == "TIME")//判断时间输入格式
            {
                int i=lbl.Text.IndexOf(".");
                if (i >= 0)
                {
                    string s=lbl.Text.Substring(i+1);
                    if (s.Length > 2) s = s.Substring(0 , 2);
                    if (Convert.ToInt32(s) > 59)
                        lbl.Text = lbl.Text.Substring(0 , i) + ".59";
                    else
                        lbl.Text = lbl.Text.Substring(0 , i) + "." + s;
                }
            }
        }

        private Label GetItemBox(string txt , Size sz , Point pt , Item tag)
        {
            return new Label()
            {
                Text = txt ,
                AutoSize = false ,
                Size = sz ,
                TextAlign = ContentAlignment.MiddleCenter ,
                BorderStyle = BorderStyle.FixedSingle ,
                Location = pt ,
                Tag = tag ,
                Font = new Font("微软雅黑" , 15f , FontStyle.Bold) ,
                BackColor = Color.PaleTurquoise ,
                ForeColor = Color.FromArgb(64 , 64 , 64) ,
            };
        }
        private Label GetItemLabel(string txt , Size sz , Point pt)
        {
            return new Label()
            {
                Text = txt ,
                AutoSize = false ,
                Size = sz ,
                TextAlign = ContentAlignment.MiddleCenter ,
                BorderStyle = BorderStyle.FixedSingle ,
                Location = pt ,
                Font = netForms.ft
            };
        }


        private void selectButton_Click(object sender , EventArgs e)
        {
            txt_SetValue.Parent = this;
            Button btn=sender as Button;
            lbl_SetTitle.Text = btn.Text;
            setButton setbutton = btn.Tag as setButton;
            SelectIndex = setbutton.btnIndex;
            if (LastSelectIndex == SelectIndex) return;
            SetButtonBackColor();
            pnl_Set.Controls.Clear();
            switch (SelectIndex)
            {
                case 0:
                    LoadBaisInterface(setbutton);
                    break;
                case 2:
                    LoadDiffInterface(setbutton);
                    break;
                case 1:
                    LoadOVSVInterface(setbutton);
                    break;
                case 3:
                    LoadOtherSetpointInterface(setbutton);
                    break;
                default:
                    break;
            }
            txt_SetValue.Parent = pnl_Set;
        }
        private void SetButtonBackColor()
        {
            selectButton[SelectIndex].button.BackColor = Color.Aqua;
            if(LastSelectIndex>=0) selectButton[LastSelectIndex].button.BackColor = pnl_Select.BackColor;
            LastSelectIndex = SelectIndex;
        }

        private void btn_SaveSettings_Click(object sender , EventArgs e)
        {
            switch (SelectIndex)
            {
                case 0:
                    SendBaisValues();
                    break;
                case 2:
                    SendDiffValues();
                    break;
                case 1:
                    SendOVSVValues();
                    break;
                case 3:
                    SendOtherSetpointValues();
                    break;
                default:
                    break;
            }
        }

        private void txt_SetValue_TextChanged(object sender , EventArgs e)
        {
            if (lblSelected == null) return;
            lblSelected.Text = txt_SetValue.Text;
        }

        private void txt_SetValue_KeyPress(object sender , KeyPressEventArgs e)//验证输入内容
        {
            if (txt_SetValue.Tag == null) return;
            Item itm=txt_SetValue.Tag as Item;
            int dp=itm.DP + (itm.DataType.ToUpper() == "TIME" ? 1 : 0)+(itm.DataType.ToUpper() == "REAL" ? 1 : 0);
            e.Handled = fdPublicFunction.CheckInput(dp , txt_SetValue.Text , e);
        }

        private void txt_SetValue_LostFocus(object sender , EventArgs e)//验证输入的大小
        {
            txt_SetValue.Visible = false;
            if (lblSelected == null || lblSelected.Tag == null) return;
            CheckValue(lblSelected);
        }
        #endregion

        private void Form_LimitSet_Activated(object sender, EventArgs e)
        {
            if (formLanguageIndex != MultipleLanguage.systemLanguageIndex)
            {
                formLanguageIndex = MultipleLanguage.systemLanguageIndex;
                foreach (var item in selectButton)
                {
                    item.button.Text = item.btnName[formLanguageIndex];
                }
                LastSelectIndex = -1;
                selectButton_Click(selectButton[0].button, null);
            }
        }
    }

    class setButton
    {
        public string[] btnName { get; set; }
        public Point btnLocation { get; set; }
        public string[] setTitle { get; set; } = new string[2] { "设置项", "Item" };
        public string[][] setDescription { get; set; } = new string[2][] { new string[2] { "", "" }, new string[2] { "", "" }, };
        public int btnIndex { get; private set; }

        public Button button { get; private set; }

        public Button getSetButton(int index)
        {
            btnIndex = index;
            button = new Button() {FlatStyle=FlatStyle.Flat, Text = btnName[MultipleLanguage.systemLanguageIndex], Name = $"setButton{index}", Tag = this, Location = btnLocation, Size = netForms.sz, Font = netForms.ft };
            return button;
        }
    }

}
